package von.seiji.cn.test;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;
import org.opencv.core.Point;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;

/**
 * @Author: Amasawa Seiji
 * @Since: 2021/1/21 15:44
 * @Description: 暂无描述
 */
public class T12 {

    @Test
    public void test11() throws IOException {
        String imgPath = "C:\\Users\\Administrator\\Desktop\\临时储物库\\图片\\图片\\helmet\\safety_helmet2.jpg";
        File file = new File(imgPath);
        HashMap<String, Object> map = new HashMap<>();
        map.put("image","data:image/jpeg;base64," + Base64.encode(file));
        map.put("image_type","BASE64");
        map.put("show","true");
      //  String post = HttpUtil.post("http://192.168.1.11:5001/helmet_detect", map);
        String post = HttpRequest.post("http://192.168.1.11:5001/helmet_detect").body(JSON.toJSONString(map)
                , ContentType.JSON.toString()).execute().body();
        JSONObject object = JSON.parseObject(post);
        int code = object.getIntValue("code");
        if(code == 1){
            System.out.println("数据返回正确！！！");
            BufferedImage read = Thumbnails.of(file).scale(0.52).outputQuality(1.0D).asBufferedImage();
           // BufferedImage read = ImageIO.read(file);
            Graphics2D graphics = read.createGraphics();
            JSONArray data = object.getJSONArray("data");
            System.out.println("data = " + data);
            data.forEach(item -> {
                JSONObject item1 = (JSONObject) item;
                JSONArray boundingbox = item1.getJSONArray("boundingbox");
                int x1 = boundingbox.getIntValue(0);
                int y1 = boundingbox.getIntValue(1);
                int x2 = boundingbox.getIntValue(2);
                int y2 = boundingbox.getIntValue(3);
                graphics.setFont(new Font("宋体", Font.BOLD, 17));
                DecimalFormat format = new DecimalFormat();
                format.applyPattern("0.00%");
                if("hat".equalsIgnoreCase(item1.getString("type"))){
                    System.out.println("发现帽子...");
                    graphics.setColor(Color.black);
                    graphics.drawString("hat "+format.format(item1.getBigDecimal("score")),x1,y1-2);
                    graphics.setColor(Color.green);
                }else{
                    System.out.println("发现人...");
                    graphics.setColor(Color.red);
                    graphics.drawString("person "+format.format(item1.getBigDecimal("score")),x1,y1-2);
                }
                System.out.print("\tx1 = " + x1);
                System.out.print("\ty1 = " + x2);
                System.out.print("\tx2 = " + y1);
                System.out.println("\tx2 = " + y2);
                graphics.drawRect(x1,y1,x2-x1,y2-y1);
            });
            graphics.dispose();
           ImageIO.write(read,"jpg",new File("F://result.jpg"));
            System.out.println("完成！！！");
        }else{
            System.err.println("数据返回错误！！！");
        }

        // Graphics graphics = new BufferedImage().getGraphics();

        //  Graphics2D graphics1 = new BufferedImage().createGraphics();
    }

    @Test
    public void test89() {
        double a = 0.3287937534987456435;
        System.out.println(a);

    }
//E:\Seiji926\software\OpenVC\opencv430\build\java\x64
    //-Djava.library.path=E:\Seiji926\software\OpenVC\opencv440\build\java\x64
    @Test
    public void test96() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        String IMAGE_PATH = "F:\\download1.jpg";
        Mat imread = Imgcodecs.imread(IMAGE_PATH);
        boolean imwrite = Imgcodecs.imwrite("F://111.jpg", imread);
        Imgproc.rectangle(imread, new Point(0,0),new Point(200,200), new Scalar(0,255,0));
        boolean imwrite1 = Imgcodecs.imwrite("F://111.jpg", imread);
        System.out.println(imwrite1);

    }

    @Test
    public void test105() {
        String json = "{\"continuous\":true,\"nativeObj\":19376864,\"nativeObjAddr\":19376864,\"submatrix\":false}";
        Mat mat = JSON.parseObject(json, Mat.class);
        boolean imwrite = Imgcodecs.imwrite("F://11111.jpg", mat);
        System.out.println(imwrite);

    }

    @Test//判断正负的方法
    public void test124() {
        int x = -9;
        int i = (x >> 31) | (~((~x + 1) >> 31) + 1);
        //经过上面第一步和第二部后正数，负数，0对应的变化是：
        /*
        * 正数：0 | 1 =1
        * 负数：-1 | X =-1
        * 0: 0 | 0 =0
        * */
        System.out.println(i);
    }

    @Test
    public void test131() {
        int x = Integer.MIN_VALUE;
        System.out.println((x >> 31));
        System.out.println((~((~x + 1) >> 31) + 1));
        System.out.println(~0);
        System.out.println(~-1);
        System.out.println(~1);
        System.out.println(Integer.MIN_VALUE + 0xffffffff == Integer.MIN_VALUE -1);
        System.out.println(Integer.MAX_VALUE == Integer.MIN_VALUE -1);
    }

    @Test
    public void test140() {
        System.out.println(0b10000000_00000000_00000000_00000000 - 1);
        System.out.println(0b10000000_00000000_00000000_00000000);
        String s = Integer.toBinaryString(2147483647);//01111111,11111111,11111111,11111111
        String s1 = Integer.toBinaryString(-2147483648);//01111111,11111111,11111111,11111111
        System.out.println(s);
        System.out.println(s1);
        System.out.println(Math.pow(2,31) - 1 == 2147483647);
    }

    @Test
    public void test149() {
int a = -32453;

        System.out.println(~a == (a^-1));
        System.out.println(0xff);
        System.out.println(0xffffffff);
    }

    @Test
    public void test169() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat=new Mat(new Size(3,3),CvType.CV_8SC1);
        mat.put(0, 0, 1,2,3);
        mat.put(1, 0, 2,2,3);
        mat.put(2, 0, 2,2,4);
        System.out.println(mat.dump());
        double d=Core.norm(mat,Core.NORM_L1);
        System.out.println("计算结果:"+d);
        Mat mat2=new Mat(new Size(3,3),CvType.CV_8SC1);
        mat2.put(0, 0, 1,2,8);
        mat2.put(1, 0, 2,2,3);
        mat2.put(2, 0, 2,2,4);
        double d2=Core.norm(mat2,Core.NORM_L1);
        System.out.println("计算结果:"+d2);
        double ds=Core.norm(mat, mat2,Core.NORM_L1);
        System.out.println("计算结果:"+ds);
    }

    @Test
    public void test187() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat=new Mat(new Size(3,3),CvType.CV_8SC1);
        mat.put(0, 0, 1,2,3);
        mat.put(1, 0, 2,2,3);
        mat.put(2, 0, 2,2,4);
        System.out.println(mat.dump());
        double d=Core.norm(mat,Core.NORM_L2);
        System.out.println("计算结果:"+d);
        Mat mat2=new Mat(new Size(3,3),CvType.CV_8SC1);
        mat2.put(0, 0, 1,2,8);
        mat2.put(1, 0, 2,2,3);
        mat2.put(2, 0, 2,2,4);
        double d2=Core.norm(mat2,Core.NORM_L2);
        System.out.println("计算结果:"+d2);
        double ds=Core.norm(mat, mat2,Core.NORM_L2);
        System.out.println("计算结果:"+ds);
    }

    @Test
    public void test207() {

    }

}
